<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @cell-mouse-enter="handleMouseEnter"
      @cell-mouse-leave="handleMouseOut"
    >
      <el-table-column align="center" label="序号"  min-width="10%">
        <template slot-scope="scope">
          {{ (page-1)*10 + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center"  min-width="15%">
        <template slot-scope="scope">
          <span v-if="!scope.row.edit">{{ scope.row.camera_name }}</span>
          <span v-if="scope.row.edit" class="cell-edit-input">
            <el-input v-model="scope.row.camera_name" placeholder="请输入设备名称"/>
          </span>
          <span v-if="!scope.row.edit" style="margin-left:10px;" class="cell-icon" @click="handleEdit(scope.$index,scope.row)">
            <i class="el-icon-edit"/>
          </span>
          <span v-if="scope.row.edit" style="margin-left:10px;" class="cell-icon" @click="handleSave(scope.$index,scope.row)">
            <i class="el-icon-document" />
          </span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center"  min-width="30%">
        <template slot-scope="scope">
          <span>{{ scope.row.deviceid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通道数" align="center"  min-width="15%">
        <template slot-scope="scope">
          <span>{{ scope.row.sumnum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center"  min-width="15%">
        <template slot-scope="scope">
          {{ scope.row.online?"在线":"离线" }}
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center" min-width="45%">
        <template slot-scope="scope">
          <el-button type="primary" @click="channels(scope.row.deviceid)" class="el-icon-arrow-right" size="mini">查看通道</el-button>
          <el-button @click="catalog(scope.row.deviceid)" class="el-icon-refresh" size="mini">更新通道</el-button>
          <el-button type="danger" @click="del(scope.row.deviceid)" class="el-icon-delete" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center;margin-top: 30px;">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="current_change">
      </el-pagination>
    </div>
  </div>

</template>

<script>
import { list, catalog, delcamera, updatename } from '@/api/camera'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      pagesize: 10,
      page: 1,
      listLoading: true
    }
  },

  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const data = { page: this.page, pagesize: this.pagesize }
      list(data).then(response => {
        this.list = response.data.items
        this.list.forEach((item) => {
          item.edit = false
        })
        this.listLoading = false
        this.total = response.data.total
      })
    },
    current_change: function(page) {
      this.page = page
      this.fetchData()
    },
    // 进入子通道
    channels(id) {
      this.$router.push({ path: '/camera/channel', query: { account: id }})
    },
    // 刷新目录
    catalog(id) {
      this.listLoading = true
      catalog({ account: id }).then(response => {
        console.log('catalog response', response)
        this.listLoading = false
      })
    },

    del(id) {
      this.$confirm('确认删除此通道？').then(_ => {
        this.listLoading = true
        delcamera({ account: id }).then(response => {
          this.listLoading = false
          this.fetchData()
        })
      }).catch(_ => {

      })
    },
    handleClose(done) {
      done()
    },
    handleEdit(index, row) {
      this.list[index].edit = true
    },
    handleSave(index, row) {
      updatename({deviceid: row.deviceid, camera_name: row.camera_name}).then(()=>{
        console.log('保存')
        this.list[index].edit = false
        this.$forceUpdate()
      })
    },
    handleMouseEnter(row, column, cell, event) {
      if(column.label!='名称')return;
      if (cell.children[0].children[1]) {
        cell.children[0].children[1].style.color = 'black'
      }
    },
    handleMouseOut(row, column, cell, event) {
      if(column.label!='名称')return;
      if (cell.children[0].children[1]) {
        cell.children[0].children[1].style.color = '#ffffff'
      }
    }
  }
}
</script>

<style>
  .video-js .vjs-icon-placeholder {
      width: 100%;
      height: 100%;
      display: block;
  }
  .cell-edit-input.el-input,
  .el-input__inner {
  width:100px;
  }
  .cell-icon{
  cursor:pointer;
  color:#fff;
  }

</style>
